using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery;

namespace DeviceDiscoveryApplication
{
    public partial class Form1 : Form
    {
        List<Device> deviceList= new List<Device>();
        DeviceType deviceType ;
        
        public Form1()
        {
            InitializeComponent();
            txtDeviceName.Enabled = false;
            cmbDeviceType.Enabled = false;
        }
       
        private void btnExit_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            lstDevices.Items.Clear();
            lstDeviceInfo.Items.Clear();            
            txtDeviceName.Text = "";
            cmbDeviceType.SelectedIndex = -1;
            cmbDiscover.SelectedIndex = -1;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                btnCancel.Enabled = false;
                DeviceDiscovery();
            }
            finally
            {
                btnCancel.Enabled = true;
                this.Cursor = Cursors.Default;
            }
        }

        private void lstDevices_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstDeviceInfo.Items.Clear();
            if (deviceList != null)
            {
                foreach (Device device in deviceList)
                {
                    if (lstDevices.Items.Contains(device.Name) && device.Name == lstDevices.SelectedItem.ToString())
                    {
                        lstDeviceInfo.Items.Add("Name : " + device.Name);
                        lstDeviceInfo.Items.Add("Address : " + device.Address.Address.ToString());
                        lstDeviceInfo.Items.Add("Type : " + device.Type.ToString());
                    }
                }
            }
        }

        private void cmbDeviceType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cmbDeviceType.Text)
            {
                case "CellPhone":
                    deviceType = DeviceType.CellPhone;                    
                    break;                
                case "DesktopComputer":
                    deviceType = DeviceType.DesktopComputer;
                    break;    
                case "Computer":
                    deviceType = DeviceType.Computer;
                    break;
                case "CordlessPhone":
                    deviceType = DeviceType.CordlessPhone;
                    break;
                case "HandheldComputer":
                    deviceType = DeviceType.HandheldComputer;
                    break;
                case "ImagingCamera":
                    deviceType = DeviceType.ImagingCamera;
                    break;
                case "ImagingDisplay":
                    deviceType = DeviceType.ImagingDisplay;
                    break;
                case "ImagingPrinter":
                    deviceType = DeviceType.ImagingPrinter;
                    break;
                case "ImagingScanner":
                    deviceType = DeviceType.ImagingScanner;
                    break;
                case "LaptopComputer":
                    deviceType = DeviceType.LaptopComputer;
                    break;               
                case "PdaComputer":
                    deviceType = DeviceType.PdaComputer;
                    break;
                case "Peripheral":
                    deviceType = DeviceType.Peripheral;
                    break;
                case "PeripheralCardReader":
                    deviceType = DeviceType.PeripheralCardReader;
                    break;
                case "PeripheralDigitizerTablet":
                    deviceType = DeviceType.PeripheralDigitizerTablet;
                    break;
                case "PeripheralGamepad":
                    deviceType = DeviceType.PeripheralGamepad;
                    break;
                case "PeripheralJoystick":
                    deviceType = DeviceType.PeripheralJoystick;
                    break;
                case "PeripheralKeyboard":
                    deviceType = DeviceType.PeripheralKeyboard;
                    break;
                case "PeripheralPointingDevice":
                    deviceType = DeviceType.PeripheralPointingDevice;
                    break;
                case "PeripheralRemoteControl":
                    deviceType = DeviceType.PeripheralRemoteControl;
                    break;
                case "PeripheralSensingDevice":
                    deviceType = DeviceType.PeripheralSensingDevice;
                    break;
                case "Phone":
                    deviceType = DeviceType.Phone;
                    break;
                case "ServerComputer":
                    deviceType = DeviceType.ServerComputer;
                    break;
                case "SmartPhone":
                    deviceType = DeviceType.SmartPhone;
                    break;
                case "WearableComputer":
                    deviceType = DeviceType.WearableComputer;
                    break;
                case "WiredPhone":
                    deviceType = DeviceType.WiredPhone;
                    break;
            }
        }

        private void cmbDiscover_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstDeviceInfo.Items.Clear();
            lstDevices.Items.Clear();
            txtDeviceName.Text = "";

            if (cmbDiscover.Text != "")
            {
                if (cmbDiscover.Text == "Discover All Devices")
                {
                    txtDeviceName.Enabled = false;
                    cmbDeviceType.Enabled = false;
                }
                else if (cmbDiscover.Text == "Discover Devices By Type")
                {
                    txtDeviceName.Enabled = false;
                    cmbDeviceType.Enabled = true;
                }
                else if (cmbDiscover.Text == "Discover Devices By Name")
                {
                    txtDeviceName.Enabled = true;
                    cmbDeviceType.Enabled = false;
                }
            }
        }

        private void DeviceDiscovery()
        {
            lstDeviceInfo.Items.Clear();
            lstDevices.Items.Clear();
            
            if (cmbDiscover.SelectedIndex != -1)
            {
                if (cmbDiscover.SelectedItem.ToString() == "Discover All Devices")
                {
                    try
                    {
                        txtDeviceName.Enabled = false;
                        cmbDeviceType.Enabled = false;
                        deviceList.Clear();                        
                        deviceList = deviceServicesManager1.DiscoverAllDevices();
                        if (deviceList != null)
                        {
                            foreach (Device device in deviceList)
                            {
                                lstDevices.Items.Add(device.Name);
                            }
                        }
                        else
                        {
                            MessageBox.Show("No device was found.",
                                            "Device Discovery", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    catch
                    {
                        MessageBox.Show("An unexpected error was raised during device discovery",
                            "Device Discovery", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                  }
                else if (cmbDiscover.SelectedItem.ToString() == "Discover Devices By Type")
                {

                    try
                    {
                        txtDeviceName.Enabled = false;

                        deviceList.Clear();
                        if (cmbDeviceType.SelectedIndex != -1)
                        {
                            deviceList = deviceServicesManager1.DiscoverDeviceByType(deviceType);
                            if (deviceList != null)
                            {
                                foreach (Device device in deviceList)
                                {
                                    lstDevices.Items.Add(device.Name);
                                }
                            }
                            else
                            {
                                MessageBox.Show("No device was found.", "Device Discovery",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                        else
                        {
                            MessageBox.Show("Please select a valid device type", "Device Discovery",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                            cmbDeviceType.Focus();
                        }
                    }
                    catch
                    {
                        MessageBox.Show("An unexpected error was raised during device discovery",
                            "Device Discovery", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else if (cmbDiscover.SelectedItem.ToString() == "Discover Devices By Name")
                {

                    try
                    {
                        deviceList.Clear();
                        cmbDeviceType.Enabled = false;
                        if (txtDeviceName.Text.Trim() != "")
                        {
                            Device device = deviceServicesManager1.DiscoverDeviceByName(txtDeviceName.Text);
                            if (device != null)
                            {
                                deviceList.Add(device);
                                lstDevices.Items.Add(device.Name);
                            }
                            else
                            {
                                MessageBox.Show("No device was found",
                                               "Device Discovery", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                        else
                        {
                            MessageBox.Show("Please enter a valid device name", "Device Discovery", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            txtDeviceName.Focus();
                        }
                    }
                    catch
                    {
                        MessageBox.Show("An unexpected error was raised during device discovery", "Device Discovery", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
    }
}